package cn.java79.springboot.zsb.ctrl;

import cn.java79.springboot.zsb.entity.User;
import cn.java79.springboot.zsb.pojo.AjaxPage;
import cn.java79.springboot.zsb.pojo.AjaxResult;
import cn.java79.springboot.zsb.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
//@CrossOrigin("*")
@RequestMapping("user")
@Api("用户管理")
public class UserController {
    private static final int PAGESIZE = 10;
    @Autowired
    private UserService userService;
    @ApiOperation("用户增加")
    @PostMapping("add")
    //RequestBody请求的数据是json格式
    public AjaxResult add(@RequestBody User u) throws IOException {
//        BufferedReader br=new BufferedReader(reader);
//        String str=null;
//        String s2="";
//        while ((str=br.readLine())!=null){
//            s2+=str;
//        }
//        System.out.println(s2);
        //System.out.println(u+"???????????????????");
        boolean b = this.userService.save(u);

        return AjaxResult.judge(b);
    }

    @ApiOperation("读取单个用户的信息")
    @GetMapping("get-{id}")
    public AjaxResult get(@PathVariable Long id) {
        User user = this.userService.getById(id);
        return AjaxResult.judge(user);
    }
    @ApiOperation("删除单个用户")
    @DeleteMapping("del/{id}")
    public AjaxResult del(@PathVariable Long id) {
        boolean user = this.userService.removeById(id);
        return AjaxResult.judge(user);
    }
    @ApiOperation("删除多个用户")
    @PostMapping("batchRemove")
    public AjaxResult delBatch(@RequestBody Long []id) {
       boolean b = this.userService.removeByIds(Arrays.asList(id));//List ->User
        return AjaxResult.judge(b);
    }
    @ApiOperation("更新单个用户信息")
    @PostMapping("update")
    public AjaxResult update(@RequestBody User u) {
        boolean b = this.userService.updateById(u);
        return AjaxResult.judge(b);
    }
    @ApiOperation("搜索用户信息")
    @GetMapping("search")
    public AjaxPage<User> search(@ApiParam("姓名") String sname,@ApiParam("手机") String phone,@ApiParam("学校名") String school,@ApiParam("页码") @RequestParam(name = "page", defaultValue = "1") int pageNo, @ApiParam("页大小")@RequestParam(name = "limit", defaultValue = "10") int limit) {
        return AjaxPage.convert(this.userService.search(sname, phone, school, pageNo, limit));
    }
    @ApiOperation("检查手机号是否注册了")
    @GetMapping("checkPhone")
    public AjaxResult checkPhone(@ApiParam("编号，可不填") Long id ,@ApiParam("手机号，必须")@RequestParam(required = true,name = "phone") String phone) {

        return AjaxResult.judge(!this.userService.checkPhone(id,phone));
    }
    @ApiOperation("重置密码")
    @PostMapping("resetpass")
    public AjaxResult resetpass(@RequestParam(required = true,name = "id") Long id,@RequestParam(required = true,name = "newPass") String newPass){
        this.userService.updatePass(id,newPass);
        return AjaxResult.success();
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
